package com.black.apartment.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.black.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.black.apartment.domain.RoomAttributes;
import com.black.apartment.mapper.RoomsMapper;
import com.black.apartment.domain.Rooms;
import com.black.apartment.service.IRoomsService;

/**
 * 房间管理Service业务层处理
 * 
 * @author kuan
 * @date 2024-10-08
 */
@Service
public class RoomsServiceImpl implements IRoomsService 
{
    @Autowired
    private RoomsMapper roomsMapper;

    /**
     * 查询房间管理
     * 
     * @param id 房间管理主键
     * @return 房间管理
     */
    @Override
    public Rooms selectRoomsById(Long id)
    {
        return roomsMapper.selectRoomsById(id);
    }

    /**
     * 查询房间管理列表
     * 
     * @param rooms 房间管理
     * @return 房间管理
     */
    @Override
    public List<Rooms> selectRoomsList(Rooms rooms)
    {
        return roomsMapper.selectRoomsList(rooms);
    }

    /**
     * 新增房间管理
     * 
     * @param rooms 房间管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRooms(Rooms rooms)
    {
        int rows = roomsMapper.insertRooms(rooms);
        insertRoomAttributes(rooms);
        return rows;
    }

    /**
     * 修改房间管理
     * 
     * @param rooms 房间管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRooms(Rooms rooms)
    {
        roomsMapper.deleteRoomAttributesByRoomId(rooms.getId());
        insertRoomAttributes(rooms);
        return roomsMapper.updateRooms(rooms);
    }

    /**
     * 批量删除房间管理
     * 
     * @param ids 需要删除的房间管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRoomsByIds(Long[] ids)
    {
        roomsMapper.deleteRoomAttributesByRoomIds(ids);
        return roomsMapper.deleteRoomsByIds(ids);
    }

    /**
     * 删除房间管理信息
     * 
     * @param id 房间管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRoomsById(Long id)
    {
        roomsMapper.deleteRoomAttributesByRoomId(id);
        return roomsMapper.deleteRoomsById(id);
    }

    /**
     * 新增房屋属性信息信息
     * 
     * @param rooms 房间管理对象
     */
    public void insertRoomAttributes(Rooms rooms)
    {
        List<RoomAttributes> roomAttributesList = rooms.getRoomAttributesList();
        Long id = rooms.getId();
        if (StringUtils.isNotNull(roomAttributesList))
        {
            List<RoomAttributes> list = new ArrayList<RoomAttributes>();
            for (RoomAttributes roomAttributes : roomAttributesList)
            {
                roomAttributes.setRoomId(id);
                list.add(roomAttributes);
            }
            if (list.size() > 0)
            {
                roomsMapper.batchRoomAttributes(list);
            }
        }
    }
}
